﻿using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZSModalWindow
{
    [DefaultProperty("Content")]
    [ToolboxData("<{0}:ZSModalWindow runat=server></{0}:ZSModalWindow>")]
    public class ZSModalWindow : Control
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public String WindowTitle
        {
            get
            {
                String s = (String)ViewState["WindowTitle"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }
            set
            {
                ViewState["WindowTitle"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        public Panel Content
        {
            get
            {
                Panel s = (Panel)ViewState["Content"];
                return ((s == null) ? new Panel() : s);
            }
            set
            {
                ViewState["Content"] = value;
            }
        }


        public String OnShow
        {
            get
            {
                String s = (String)ViewState["OnShow"];
                return (String.IsNullOrEmpty(s) ? String.Empty : s);
            }
            set
            {
                ViewState["OnShow"] = value;
            }
        }


        public String OnHide
        {
            get
            {
                String s = (String)ViewState["OnHide"];
                return (String.IsNullOrEmpty(s) ? String.Empty : s);
            }
            set
            {
                ViewState["OnHide"] = value;
            }
        }


        public Boolean AutoOpen
        {
            get
            {
                var s = Convert.ToBoolean(ViewState["AutoOpen"]);
                return s;
            }
            set
            {
                ViewState["AutoOpen"] = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {


            String cssStyles = @"<style type=""text/css"">
    .modal-window {position: absolute; top: 0pt; left: 0pt; width: 100%; height: 100%; display:none;}
    .modal-window {-moz-text-align: center}
    .modal-window-background {position: fixed; top: 0pt; left: 0pt; height: 100%; width: 100%; text-align: center;z-index: 4; background-color: black; opacity: 0.7; filter: alpha(opacity=70)}
    .modal-window-heading {float: left; margin-top: 10px; margin-left: 10px; border: 0px solid rgb(255, 153, 0);}
    .modal-window-close-button {}
    .modal-window-content-container {position: relative; top: 50px; z-index: 402; width: 650px; border: 2px solid rgb(204, 204, 204);background-color: rgb(255, 255, 255);}
    .modal-window-body {}
    </style>";

            this.Page.Header.Controls.Add(new Literal() { Text = cssStyles });



            String javascriptLibrary = @"<script type=""text/javascript"">
function ZSModalWindow(modalWindowId) {
    this.clientId = modalWindowId;this.onShow = function () { };this.onHide = function () { };
    this.show = function (callback) {
        var modal = document.getElementById(this.clientId);if (modal != null) { modal.style.display = ""block""; }
        if (callback) { callback(); } if (this.onShow) { this.onShow(); }
    }
    this.hide = function (callback) {
        var modal = document.getElementById(this.clientId);if (modal != null) { modal.style.display = ""none""; }
        if (callback) { callback(); }if (this.onHide) { this.onHide(); }
    }
}
            </script>";
            javascriptLibrary = javascriptLibrary + Environment.NewLine;
            javascriptLibrary = javascriptLibrary + Environment.NewLine;

            this.Page.Header.Controls.Add(new Literal() { Text = javascriptLibrary });



            base.OnLoad(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<center>");


            String clientId = this.ClientID; //"modalwindow_" + this.ClientID;


            #region render html
            writer.Write("<div class=\"modal-window\" id=\"" + clientId + "\"> ");
            writer.Write("  <div class=\"modal-window-background\"></div>");
            writer.Write("  <div class=\"modal-window-content-container\">");
            writer.Write("      <div class=\"modal-window-heading\">" + this.WindowTitle + "</div>");
            writer.Write("      <div style=\"float: right; padding-top: 5px; padding-right: 5px;\">");
            writer.Write("          <a href=\"#\" onclick=\"" + clientId + ".hide();\" class=\"modal-window-close-button\">x</a>");
            writer.Write("      </div>");
            writer.Write("      <br class=\"ClearBoth\" />");
            writer.Write("      <div class=\"modal-window-body\">");

            base.Render(writer);

            writer.Write("      </div>");
            writer.Write("  </div>");
            writer.Write("</div>");
            writer.Write("</center>");
            #endregion




            #region Create Javascript for Modal Window
            StringBuilder javascriptBuilder = new StringBuilder();

            // create object of modal window
            javascriptBuilder.AppendLine("  var " + clientId + " = new ZSModalWindow('" + clientId + "');");

            if (!String.IsNullOrEmpty(this.OnShow))
                javascriptBuilder.AppendLine("  " + clientId + ".onShow = " + this.OnShow + ";");

            if (!String.IsNullOrEmpty(this.OnHide))
                javascriptBuilder.AppendLine("  " + clientId + ".onHide = " + this.OnHide + ";");

            //// register the onshow function
            //if (!String.IsNullOrEmpty(this.OnShow))
            //{
            //    javascriptBuilder.AppendLine(Environment.NewLine);
            //    javascriptBuilder.AppendLine("  if (typeof(" + this.OnShow + ") == 'function') {");
            //    javascriptBuilder.AppendLine("    " + this.OnShow + "();");
            //    javascriptBuilder.AppendLine("  }");
            //}

            //javascriptBuilder.AppendLine("}");



            //javascriptBuilder.AppendLine("function hide(){");
            //javascriptBuilder.AppendLine("  var modal = document.getElementById('" + clientId + @"');");
            //javascriptBuilder.AppendLine("  if(modal!=null){");
            //javascriptBuilder.AppendLine("      modal.style.display = \"none\";");
            //javascriptBuilder.AppendLine("  }");

            //if (!String.IsNullOrEmpty(this.OnHide))
            //{
            //    javascriptBuilder.AppendLine(Environment.NewLine);
            //    javascriptBuilder.AppendLine("  if (typeof(" + this.OnHide + ") == 'function') {");
            //    javascriptBuilder.AppendLine("    " + this.OnHide + "();");
            //    javascriptBuilder.AppendLine("  }");
            //}

            //javascriptBuilder.AppendLine("}");


            String autoOpenStyle = String.Empty;
            if (this.AutoOpen)
            {
                javascriptBuilder.AppendLine(Environment.NewLine);
                javascriptBuilder.AppendLine("// auto open modal window");
                javascriptBuilder.AppendLine("" + clientId + ".show();");
            }


            String javascript = javascriptBuilder.ToString();
            Page.ClientScript.RegisterStartupScript(typeof(Page), "CustomModalWindowKey", javascript, true);
            #endregion



        }

    }
}
